// packageJ/anytime_service/my/skill_manage/skill_manage.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    language: "",
    page:1,
    last_page:1,
    active:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en
    });
    this.getData();
  },
  tab_handle(e){
    this.setData({active:e.detail.name});
    this.getData()
  },
  //派单/取消派单通知，技能审核通知
  //取消派单通知（师傅）
  //获取模板ID
  getTemplate(event) {
    let {i,aid,status} = event.currentTarget.dataset;
    let urlStr = app.getNetAddresss("plugin.be-within-call.frontend.notice-template");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          let array = [];
          let data = res.data;
          for(let key in data) {
            console.log(key);
            if(data[key].key  == 'skill_examine_master' || data[key].key  == 'send_order_master' || data[key].key  == 'cancel_order_master') {
              if(data[key].is_open) {
                array.push(data[key].template_id);
              }
            }
          }
          this.setData({
            TemplateArray: array,
          });
          wx.requestSubscribeMessage({
            tmplIds: this.data.TemplateArray,
            success: (res) => {
              console.log(res,'resresresresresres');
              if (i == 1) {
                if(status==-1){
                  wx.navigateTo({
                    url: '/packageJ/anytime_service/my/skill_release/skill_release?type=2&aid='+aid,
                  })
                }else{
                  wx.navigateTo({
                    url: '/packageJ/anytime_service/my/skill_release/skill_release?type=1&aid='+aid,
                  })
          
                }
              } else {
                wx.navigateTo({
                  url: '/packageJ/anytime_service/my/skill_release/skill_release?type=2',
                })
              }
            },
            fail: (err) => {
              console.log(err,'errerrerrerrerr');
              if (i == 1) {
                if(status==-1){
                  wx.navigateTo({
                    url: '/packageJ/anytime_service/my/skill_release/skill_release?type=2&aid='+aid,
                  })
                }else{
                  wx.navigateTo({
                    url: '/packageJ/anytime_service/my/skill_release/skill_release?type=1&aid='+aid,
                  })
          
                }
              } else {
                wx.navigateTo({
                  url: '/packageJ/anytime_service/my/skill_release/skill_release?type=2',
                })
              }
            },
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  getData(page = 1){
    let json = {
      page,
      state:this.data.active
    }
    app._getNetWork({
      url:app.getNetAddresss('plugin.be-within-call.frontend.shi-fu.ability'),
      data:json,
      success:resdata=>{
        let {result,data,msg} = resdata.data;
        if(result){

          this.page = data.current_page
          this.last_page = data.last_page;
          if(page == 1){
            this.setData({
              list:data.data
            })
          }else{
            this.setData({
              list:this.data.list.concat(data.data)
            })
          }


        }else {
          app.tips(msg);
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  submit(e) {
    // if (e.currentTarget.dataset.i == 1) {
    //   let aid = e.currentTarget.dataset.aid;
    //   if(e.currentTarget.dataset.status==-1){
    //     wx.navigateTo({
    //       url: '/packageJ/anytime_service/my/skill_release/skill_release?type=2&aid='+aid,
    //     })
    //   }else{
    //     wx.navigateTo({
    //       url: '/packageJ/anytime_service/my/skill_release/skill_release?type=1&aid='+aid,
    //     })

    //   }
    // } else {
    //   wx.navigateTo({
    //     url: '/packageJ/anytime_service/my/skill_release/skill_release?type=2',
    //   })
    // }
    this.getTemplate(e);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.page>=this.data.last_page){
      return;
    }
    let page = this.data.page + 1;
    this.getData(page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})